<template>
  <div class="editForm-wrapper">
    <!-- 磁卡注册编辑dialog-->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="editVisible"
    >
      <el-form
        label-width="92px"
        :rules="dataRule"
        ref="editForm"
        :model="editForm"
      >
        <el-form-item label="卡类型:" prop="cardType">
          <el-select clearable v-model="editForm.cardType" placeholder="请选择">
            <el-option
              v-for="item in  $dictUtils.getDictList('cardType')"
              :key="item.value"
              :label="item.label"
              :value="Number(item.value)">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="卡内号:" prop="cardInnerNumber">
          <el-input v-model="editForm.cardInnerNumber" placeholder="点击读卡按钮自动获取"></el-input><el-link style="margin-left: 10px" type="primary" :underline="false" @click="readCard">读卡</el-link>
        </el-form-item>
        <el-form-item label="卡序号:" prop="cardNumber">
          <el-input v-model="editForm.cardNumber" placeholder="请输入卡片表面序号"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footer-input">
          <el-button size="small" type="primary" @click="doSubmit()" v-noMoreClick
            >确定</el-button
          >
          <el-button size="small" @click="close()">取消</el-button>

        </div>

      </span>
    </el-dialog>
  </div>
</template>
<script>
import { edit, queryById } from '@/api/truck/cardRegiste/cardRegiste.js'
export default {
  components: {},
  data () {
    return {
      method: '',
      title: '',
      editForm: {
        cardType: '',
        cardInnerNumber: '',
        cardNumber: '',
        state: '',
        cardUseage: '',
        id: ''
      },
      viewForm: {
        cardType: '',
        cardInnerNumber: '',
        cardNumber: '',
        state: '',
        id: ''
      },
      editVisible: false,
      detailsVisible: false,
      dataRule: {
        cardType: [
          { required: true, message: '请选择磁卡类型', trigger: 'blur' }
        ],
        cardInnerNumber: [
          { required: true, message: '卡序号不能为空', trigger: 'blur' }
        ],
        cardNumber: [
          { required: true, message: '卡面编号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
  	handleClose(done) {
		this.$Msg.confirm({
			title:'您确定要关闭吗？',
			content:this.GLOBAL.FORM_CLOSE_TIPS,
			confirmCall:()=>{
				done();
			}
		})
	},
    init (method, editForm) {
      if (method === 'add') {
        this.editVisible = true
        this.$nextTick(() => {
          this.$refs['editForm'].resetFields()
        })
        this.editForm.id = ''
        this.title = '新增磁卡'
      } else if (method === 'edit') {
        this.editVisible = true
        this.method = method
        this.$nextTick(() => {
          this.$refs.editForm.clearValidate()
          // this.editForm = editForm
          queryById({
            id: editForm
          }).then(({data}) => {
            if (data.code === '200') {
              this.editForm = data.result
            }
          })
        })

        this.title = '修改磁卡'
      } else if (method === 'view') {
        this.method = method
        this.detailsVisible = true
        this.viewForm = editForm
        this.$nextTick(() => {
          this.$refs.editForm.resetFields()
          this.editForm = editForm
        })
        this.title = '磁卡注册详情'
      }
    },
    close () {
      this.editForm = {}
      this.editVisible = false
    },
    readCard () {
      this.editVisible = true
      this.editForm.cardInnerNumber = '879886'
    },
    doSubmit () {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          edit(this.editForm).then(({data}) => {
            if (data.code === '200') {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.editVisible = false
              this.$emit('refreshDataList')
            } else {
              this.$message.error(data.message)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>

.editForm-wrapper{
    ::v-deep.el-dialog{
      .el-input__inner{
        width: 320px;
      }
    }
  }
.view-container {
  margin-bottom: 20px;
  display: flex;
  .label-container {
    width: 150px;
    text-align: right;
    font-size: 16px;
    font-weight: 400;
    color: #666666;
  }
  .form-container {
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    width: 200px;
  }
}
</style>
